#include <stdlib.h>
#include <stdio.h>

#include "queue.h"

void beep()
{	
	Note n = {Do6, 300};
	
	uint divisor = TIMER_CLK / notes_frequency[n.freq];
	
	outportb(TIMER_2, LSB(divisor));
	outportb(TIMER_2, MSB(divisor));
	speaker_on();

	delay(n.dur); /* wait for note duration */
	speaker_off();	
}

void queueInit(Queue *q)
{
	q->in = q->out = 0;
	q->cnt = 0;
	q->size = 10;
	q->buf = malloc(q->size * sizeof(char));
}

Bool queuePut(Queue *q, char c)
{
	if (queueFull(q)) {
		return false;
	}
	else {	
		q->buf[q->in] = c;
		
		/* incrementa para a proxima posicao */
		q->in++;

		/* incrementar o numero de elementos da queue */
		q->cnt++;
		
		if(q->in == q->size)
			q->in = 0;
	}
	
	return true;
}

char queueGet(Queue *q)
{
	if (queueEmpty(q))
		return -1;
	else {
		char c = q->buf[q->out];
		q->out++;
		q->cnt--;
		if(q->out == q->size)
			q->out = 0;
		return c;
	}
}

Bool queueEmpty(Queue *q)
{
	if (q->cnt == 0)
		return true;
		
	return false;
}

Bool queueFull(Queue *q)
{
	if (q->cnt == q->size)
		return true;
		
	return false;
}
